import { _decorator, Component, instantiate, ParticleSystem, Prefab } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('TankHealth')
export class TankHealth extends Component {
    @property
    public stratingHealth = 100;
    @property(Prefab)
    public explosionPrefab: Prefab = null;
    public explosionParticle: ParticleSystem = null;

    private currentHealth = 0;

    private isdead = false;

    start() {
        let explosionPrefab = instantiate(this.explosionPrefab);
        explosionPrefab.setParent(this.node.parent);

        this.explosionParticle = explosionPrefab.getComponent(ParticleSystem);
        this.currentHealth = this.stratingHealth;
        this.isdead = false;
        this.SetHealthUI();
    }

    public takeDamage(ammout: number) {
        this.currentHealth -= ammout;
        this.SetHealthUI();
        if (this.currentHealth <= 0 && !this.isdead) {
            this.onDeath();
        }
        console.log("hurt" + this.currentHealth);
    }


    SetHealthUI() { }

    onDeath() {
        this.isdead = true;
        this.explosionParticle.node.worldPosition = this.node.worldPosition;
        this.explosionParticle.play();
        this.node.active = false;
        this.node.destroy();
    }

}